import json
import pandas as pd

# 读取JSON文件
file_path = "questions2.json"
# 将数据写入 Excel 文件
# file_name = "填空题.xlsx"
question_type = "选择题"
file_name = f"{question_type}.xlsx"
with pd.ExcelWriter(file_name, engine='xlsxwriter') as writer:
    with open(file_path, 'r', encoding='utf-8') as file:
        data = json.load(file)

    # 创建空的数据列表
    data_list = []
    print(len(data))
    for idx, item in enumerate(data, start=1):
        # data_dict = {
        #     "序号": idx,
        #     "题型*": "填空题",
        #     "题干*": item["stem"],
        #     "难度等级*": "",
        #     "答案*": ", ".join(item["answer"]),
        #     "答案解析": ", ".join(item["analyze"]),
        #     "知识点": item["knowledge"]
        # }
        # data_dict = {
        #     "序号": idx,
        #     "题型*": question_type,
        #     "题干*": item["stem"],
        #     "答案*": ", ".join(item["answer"]),
        #     "答案解析": ", ".join(item["analyze"]),
        #     "知识点": item["knowledge"]
        # }
        data_dict = {
            "序号": idx,
            "题型*": "选择题",
            "题干*": item["stem"],
            "难度等级*": "",
            "答案*": ", ".join(item["answer"]),
            "选项A*": item["Options"][0],
            "选项B*": item["Options"][1],
            "选项C": item["Options"][2],
            "选项D": item["Options"][3],
            "选项E": "",
            "选项F": "",
            "答案解析": ", ".join(item["analyze"]),
            "知识点": item["knowledge"]
        }

        data_list.append(data_dict)

    # 将数据列表转换为数据框
    df = pd.DataFrame(data_list)

    # 将数据写入Excel文件，放在一个工作表中
    df.to_excel(writer, sheet_name=question_type, index=False)

print(f"Excel 文件 '{file_name}' 已生成")
